#include "VEditor.h"

VEditor::VEditor(QWidget *parent) : QWidget(parent)
{
    int i;

    this->setFixedSize(1000, 600);

    this->editor_data = new CEditor();

    this->current_grid = 0;

    this->create_new_building_label = new QLabel();
    this->create_new_building_pixmap = new QPixmap(MBuilding::image);
    this->create_new_building_label->setPixmap(*(this->create_new_building_pixmap));
    this->create_new_building = new QPushButton("Batiment");

    this->create_new_gate_label = new QLabel();
    this->create_new_gate_pixmap = new QPixmap(MGate::image);
    this->create_new_gate_label->setPixmap(*(this->create_new_gate_pixmap));
    this->create_new_gate = new QPushButton("Porte");

    this->create_new_grid = new QComboBox();
    i = 0;
    while (VGridLabel::grids[i] != NULL)
    {
        this->create_new_grid->addItem(VGridLabel::grids_name[i]);
        i += 1;
    }

    this->create_new_map = new QPushButton("Nouveau");
    this->load_new_map = new QPushButton("Charger");

    this->save_new_map = new QPushButton("Sauvegarder");
    this->quit_editor = new QPushButton("Quitter");

    this->grid_label = new VGridLabel(this->editor_data);
    this->grid_image = new QPixmap(VGridLabel::grids[0]);
    this->grid_label->setPixmap(*(this->grid_image));

    this->tools = new QWidget();
    this->map = new QWidget();

    this->tools->setFixedWidth(200);
    this->map->setFixedWidth(800);

    this->h_layout = new QHBoxLayout();
    this->h_layout->addWidget(this->tools);
    this->h_layout->addWidget(this->map);

    this->v_layout_tools = new QVBoxLayout();
    this->v_layout_map = new QVBoxLayout();

    this->h_layout1 = new QHBoxLayout();
    this->h_layout2 = new QHBoxLayout();
    this->h_layout3 = new QHBoxLayout();

    this->h_layout1->addWidget(this->create_new_building_label);
    this->h_layout1->addWidget(this->create_new_building);
    this->h_layout1->addWidget(this->create_new_gate_label);
    this->h_layout1->addWidget(this->create_new_gate);
    this->h_layout2->addWidget(this->create_new_map);
    this->h_layout2->addWidget(this->load_new_map);
    this->h_layout3->addWidget(this->save_new_map);
    this->h_layout3->addWidget(this->quit_editor);

    this->v_layout_tools->addLayout(this->h_layout1);
    this->v_layout_tools->addWidget(this->create_new_grid);
    this->v_layout_tools->addLayout(this->h_layout2);
    this->v_layout_tools->addLayout(this->h_layout3);

    this->tools->setLayout(this->v_layout_tools);
    this->map->setLayout(this->v_layout_map);

    this->setLayout(this->h_layout);

    QObject::connect(this->create_new_building, SIGNAL(clicked()), this, SLOT(new_building()));
    QObject::connect(this->create_new_gate, SIGNAL(clicked()), this, SLOT(new_gate()));
    QObject::connect(this->create_new_grid, SIGNAL(activated(int)), this, SLOT(new_grid(int)));
    QObject::connect(this->create_new_map, SIGNAL(clicked()), this, SLOT(new_map()));
    QObject::connect(this->load_new_map, SIGNAL(clicked()), this, SLOT(load_map()));
    QObject::connect(this->save_new_map, SIGNAL(clicked()), this, SLOT(save_map()));
    QObject::connect(this->quit_editor, SIGNAL(clicked()), this, SLOT(quit()));
}

VEditor::~VEditor()
{
    delete this->editor_data;
    delete this->create_new_building_pixmap;
    delete this->create_new_building_label;
    delete this->create_new_building;
    delete this->create_new_gate_pixmap;
    delete this->create_new_gate_label;
    delete this->create_new_gate;
    delete this->create_new_grid;
    delete this->create_new_map;
    delete this->load_new_map;
    delete this->save_new_map;
    delete this->quit_editor;
    delete this->grid_image;
    delete this->grid_label;
    delete this->v_layout_tools;
    delete this->v_layout_map;
    delete this->map;
    delete this->tools;
    delete this->h_layout;
}

// Regénère une carte chargée depuis un fichier
void VEditor::recreate_map()
{
    int i;

    i = this->editor_data->working_map->grid;

    this->grid_image = new QPixmap(VGridLabel::grids[i]);
    this->grid_label->setPixmap(*(this->grid_image));
    this->v_layout_map->addWidget(this->grid_label);
    this->show_map();
}

// Crée un nouveau bâtiment
void VEditor::new_building()
{
    this->editor_data->selected_type = MCASE_BUILDING;
}

// Crée une nouvelle porte
void VEditor::new_gate()
{
    this->editor_data->selected_type = MCASE_GATE;
}

// Change la grille en cours
void VEditor::new_grid(int i)
{
    if (this->editor_data->working_map != NULL)
        this->editor_data->working_map->grid = i;
    this->current_grid = i;
    if (this->grid_image != NULL)
        delete this->grid_image;
    this->grid_image = new QPixmap(VGridLabel::grids[i]);
    this->grid_label->setPixmap(*(this->grid_image));
}

// Crée une nouvelle carte
void VEditor::new_map()
{
    this->editor_data->close_all_gates();
    if (this->editor_data->opened_gates.size() == 0 && CEditor::not_finished == 0)
    {
        this->editor_data->new_map();
        this->editor_data->working_map->grid = this->current_grid;
        this->v_layout_map->addWidget(this->grid_label);
    }
}

// Affiche une carte
void VEditor::show_map()
{
    QLabel *label;
    QPixmap *pixmap;
    int pos;

    pos = 0;
    while (pos < (80 * 60))
    {
        if ((this->editor_data->working_map->table)[pos] != NULL)
        {
            label = new QLabel(this->grid_label);
            (this->editor_data->table)[pos] = label;
            if ((this->editor_data->working_map->table)[pos]->type == MCASE_BUILDING)
                pixmap = new QPixmap(MBuilding::image);
            else if ((this->editor_data->working_map->table)[pos]->type == MCASE_GATE)
                pixmap = new QPixmap(MGate::image);
            else
            {
                delete label;
                (this->editor_data->table)[pos] = NULL;
                pos += 1;
                continue;
            }
            label->setPixmap(*pixmap);
            label->move((pos % 80) * 10, (pos / 80) * 10);
            label->show();
            delete pixmap;
        }
        pos += 1;
    }
}

// Charge une carte depuis un fichier
void VEditor::load_map()
{
    this->editor_data->close_all_gates();
    if (this->editor_data->opened_gates.size() == 0 && CEditor::not_finished == 0)
    {
        this->editor_data->load_map(QFileDialog::getOpenFileName(this, tr("Load"), "/home/Antoine/supinfo/", tr("MAP files (*.map)")));
        this->recreate_map();
    }
}

// Sauvegarde une carte
void VEditor::save_map()
{
    this->editor_data->close_all_gates();
    if (this->editor_data->opened_gates.size() == 0 && CEditor::not_finished == 0)
        this->editor_data->save_map(QFileDialog::getSaveFileName(this, tr("Save"), "/home/Antoine/supinfo/", tr("MAP files (*.map)")));
}

// Quitte l'éditeur
void VEditor::quit()
{
    // VLauncher w;
    // w.show();
    this->editor_data->close_all_gates();
    if (this->editor_data->opened_gates.size() == 0 && CEditor::not_finished == 0)
    {
        this->close();
        delete this;
    }
}
